package com.lishe.lishe.lishe.module.recharge.http;

import android.util.Log;

import com.lishe.lishe.asynchttp.HttpUtils;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.recharge.bean.RechargeRecord;
import com.lishe.lishe.lishe.utils.NetWorkUtils;
import com.lishe.lishe.retrofit.BaseRequestBody;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.lishe.lishe.retrofit.RetrofitFactory;
import com.lishe.lishe.retrofit.bean.RechargeRecordDataPageBody;
import com.lishe.lishe.retrofit.bean.WXPayObject;
import com.lishe.lishe.retrofit.interfaces.PayService;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by lwa on 2017/8/22.
 * 充值模块接口
 */

public class HttpPay {
    //数据所在的表
    private static final String TABLE_PAY = "Pay/";
    private static final String TABLE_API = "api/";

    /**
     * 充值记录接口
     * @param token
     * @param money
     * @param response
     */
    public static void rechargeRecorder(String token, double money, ResponseHandlerInterface response){
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("money", money);
        HttpUtils.post(Globals.LISHE_URL +"Home/"+TABLE_PAY + "inteRechDo/", params, response);
    }

    /**
     * 支付宝付款后——发送充值记录到服务器保存
     * @param token
     * @param paymentid 支付id
     * @param alipayRetuenData 支付宝扣款成功返回的所有数据（json）
     * @param response
     */
    public static void recharge(String token, String paymentid,String alipayRetuenData, ResponseHandlerInterface response){
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("paymentid", paymentid);
        params.put("alipayRetuenData", alipayRetuenData);
        HttpUtils.post(Globals.LISHE_URL +"Home/"+TABLE_PAY + "updatepay/", params, response);
    }

    /**
     * 支付宝积分充值预订单接口
     * @param token
     * @param subject 商品的标题/交易标题/订单标题/订单关键字等
     * @param total_amount 订单总金额
     * @param body   商品描述
     * @param goods_type  商品主类型(0 虚拟产品，1实物)
     * @param store_id  支付宝用途为商户门店编号。该参数用于请求参数中以区分各门店，非必传项。
    当前用途用于区分ios端支付或者是android
     * @param response
     */
    public static void creatOrderInfor(String token,String subject,String total_amount,String body,String goods_type,String store_id, ResponseHandlerInterface response){
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("subject", subject);
        params.put("total_amount", total_amount);
        params.put("body", body);
        params.put("goods_type", goods_type);
        params.put("store_id", store_id);
        HttpUtils.post(Globals.LISHE_URL +"Home/"+TABLE_PAY + "createPayOrderInfo/", params, response);
    }
    /**
     * 支付宝付款后——发送充值记录到服务器保存(通知服务器增加积分)
     * @param token
     * @param alpayJsonString 支付完成时支付宝返回的结果数据
     * @param response
     */
    public static void recharge(String token, String alpayJsonString, ResponseHandlerInterface response){
        Log.e("充值","接口调用");
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("alpayJsonString", alpayJsonString);
        HttpUtils.post(Globals.LISHE_URL +"Home/"+TABLE_PAY + "androidRecharge/", params, response);
    }

    /**
     * 查询充值规则
     */
    public static void searchRechage(ResponseHandlerInterface response){
        Log.e("充值","接口调用");
        RequestParams params = new RequestParams();
        HttpUtils.post(Globals.LISHE_URL_EXCITATION_U +"lshe.framework.protocol.http/api/pointActive/getAllPointActive?terminalType=APP", params, response);
    }

    /**
     * 获取充值记录列表
     * @param token
     * @param page
     * @param pageSize
     * @param listener
     */
    public static void getRechargeRecord(String token, int page, int pageSize, final DataResponseListener<RechargeRecordDataPageBody<RechargeRecord>> listener){
        RetrofitFactory.create().create(PayService.class)
                .getRechargeRecord(token,page,pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if (!NetWorkUtils.isNetworkConnected()) {
                            listener.onNetWorkError();
                        }
                    }
                })
                .subscribe(new Observer<BaseRequestBody<RechargeRecordDataPageBody<RechargeRecord>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseRequestBody<RechargeRecordDataPageBody<RechargeRecord>> requestBody) {
                        if (requestBody.getResult() == 100 && requestBody.getErrcode() == 0) {
                            listener.onSuccess(requestBody.getData());
                        } else {
                            listener.onFail(requestBody.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onError(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
